// https://zh.cppreference.com/w/cpp/thread/barrier
#include <barrier>
#include <iostream>
#include <string>
#include <syncstream>
#include <thread>
#include <vector>
using namespace std;

void on_completion();
void get_cmd();
// std::barrier barrier_point(1, on_completion);
// std::barrier barrier_point(2, on_completion);
std::barrier barrier_point(3, on_completion);

void test_thread(string id)
{
    std::cout << "Thread " << id << " started\n";
    // barrier_point.wait();
    barrier_point.arrive_and_wait();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Thread " << id << " finished\n";
}

void on_completion()
{
    std::cout << "All threads have completed\n";
}

int main()
{
    thread t1 = thread(test_thread, "lili");
    thread t2 = thread(test_thread, "huhu");
    t1.join();
    t2.join();

    return 0;
}

/* 测试1: barrier_point(2) 时 :阻塞所有线程完成
Thread lili started
Thread huhu started
All threads have completed
Thread huhu finished
Thread lili finished

 */

/* 测试2: barrier_point(1) 时 :阻塞1个线程完成, 下一个线程继续执行
Thread lili started
All threads have completed
Thread huhu started
All threads have completed
Thread lili finished
Thread huhu finished

 */

/* 测试3: barrier_point(3) 时 :只有2个线程, 全部阻塞
Thread lili started
Thread huhu started

 */